package com.hjy.backend.tableManager;


import com.hjy.backend.dataManager.DataManager;
import com.hjy.backend.parser.statement.*;
import com.hjy.backend.tableManager.result.BeginRes;
import com.hjy.backend.utils.Parser;
import com.hjy.backend.versionManager.VersionManager;

public interface TableManager {
    /**
     * begin 语句, 开始一个事务
     *
     * @param begin
     * @return
     */
    BeginRes begin(Begin begin);

    /**
     * commit 语句, 提交一个事务
     *
     * @param xid
     * @return
     * @throws Exception
     */
    byte[] commit(long xid) throws Exception;

    /**
     * 撤销一个事务
     *
     * @param xid
     * @return
     */
    byte[] abort(long xid);

    /**
     * show 语句展示现在有哪些表
     *
     * @param xid
     * @return
     */
    byte[] show(long xid);

    /**
     * create 建表语句
     *
     * @param xid
     * @param create
     * @return
     * @throws Exception
     */
    byte[] create(long xid, Create create) throws Exception;

    /**
     * insert 语句
     *
     * @param xid
     * @param insert
     * @return
     * @throws Exception
     */
    byte[] insert(long xid, Insert insert) throws Exception;

    /**
     * select 语句
     *
     * @param xid
     * @param select
     * @return
     * @throws Exception
     */
    byte[] read(long xid, Select select) throws Exception;

    /**
     * update 语句
     *
     * @param xid
     * @param update
     * @return
     * @throws Exception
     */
    byte[] update(long xid, Update update) throws Exception;

    /**
     * delete 语句
     *
     * @param xid
     * @param delete
     * @return
     * @throws Exception
     */
    byte[] delete(long xid, Delete delete) throws Exception;

    public static TableManager create(String path, VersionManager vm, DataManager dm) {
        Starter starter = Starter.create(path);
        starter.update(Parser.long2Byte(0));
        return new TableManagerImpl(vm, dm, starter);
    }

    public static TableManager open(String path, VersionManager vm, DataManager dm) {
        Starter starter = Starter.open(path);
        return new TableManagerImpl(vm, dm, starter);
    }
}
